const app = getApp()
const now = new Date()
const util = require('../../utils/util')
Page({
  data: {
    title: "",
    dese: "",
    show: "",
    num: "",
    price: "",
    local: "",
    others: "",
    dater: util.formatYMD(now),
    time: util.formatHM(now),

    index: 0,
    typearray: ['网球', '篮球', '足球', '台球', '棒球', '高尔夫球', '羽毛球', '乒乓球', '跑步', '空手道', '剑术','跳绳', '其他'],
    imagearry: [
      'https://i.loli.net/2020/06/05/AqDb1X6we4gahO2.gif',
      'https://i.loli.net/2020/06/05/GbLJq7aBhzIpZgv.gif',
      'https://i.loli.net/2020/06/05/ynbKPZrMRacFmLA.gif',
      'https://i.loli.net/2020/06/05/S1o8kyfYTHVW3Eg.gif',
      'https://i.loli.net/2020/06/05/YZWQXmPapAzbqGv.gif',
      'https://i.loli.net/2020/06/05/6WszUQmR1BlvdOx.gif',
      'https://i.loli.net/2020/06/05/Z3deWNtG4UK7mgS.gif', 
      'https://i.loli.net/2020/06/05/C2aVSND3GbREIYX.gif',
      'https://i.loli.net/2020/06/05/UKLtYx2uDogHSpm.gif',
      'https://i.loli.net/2020/06/05/3BfhvPTuLS5Rm4l.gif',
      'https://i.loli.net/2020/06/05/ANtl8xZXTJaw2Q6.gif',
      'https://i.loli.net/2020/06/05/IqUxOPalpNskWog.gif',
      'https://i.loli.net/2020/06/05/OdQmqpIb94iZBwn.gif',
    ]
  },

  bindinputTitle: function (e) {
    this.setData({
      title: e.detail.value
    })
  },
  bindinputDese: function (e) {
    this.setData({
      dese: e.detail.value
    })
  },
  bindchangeType: function (e) {
    this.setData({
      index: e.detail.value
    })
  },
  bindinputNum: function (e) {
    this.setData({
      num: e.detail.value
    })
  },
  bindinputLoacl: function (e) {
    this.setData({
      local: e.detail.value
    })
  },
  bindchangeDate: function (e) {
    this.setData({
      dater: e.detail.value
    })
  },
  bindchangeTime: function (e) {
    this.setData({
      time: e.detail.value
    })
  },
  bindinputOthers: function (e) {
    this.setData({
      others: e.detail.value
    })
  },

  sumbit: function (e) {
    const db = wx.cloud.database()
    db.collection('sport').add({
      data: {
        title: this.data.title,
        dese: this.data.dese,
        show: this.data.typearray[this.data.index],
        num: this.data.num,
        price: new Date(this.data.dater + ' ' + this.data.time),
        local: this.data.local,
        others: this.data.others,
        image: this.data.imagearry[this.data.index]
      },
      success: res => {
        console.log(res)
        this.setData({})
        wx.showToast({
          title: '信息发布成功',
          icon: 'success',
          duration: 2000
        })
        console.log('新增数据成功，记录_id:', res._id)
        wx.redirectTo({
          url: '../sport/sport',
        })
      },
      fail: err => {
        wx.showToast({
          title: '信息发布失败',
          icon: 'none'
        })
        console.error('信息发布失败', err)
      }
    })
  },
})